/**
 * 
 */
var projectContentUrl;
var itemContentUrl;
var rowNumber;
	$(function(){
		projectContentUrl = UM.getEditor('projectContentUrl');
		itemContentUrl = UM.getEditor('itemContentUrl');
		projectContentUrl.addListener('blur', function() {
			$('#focush2').html('编辑器失去焦点了')
		});
		projectContentUrl.addListener('focus', function() {
			$('#focush2').html('')
		});
	});
	$(function() {
		$('#publishitem_table')
				.datagrid(
						{
							view: detailview,
							url : 'admin/queryAdProject.do?pageSize=10',
							collapsible : true,
							width : '100%',
							height : '450px',
							iconCls : 'icon-man',
							fitColumns : true,
							pagination : true,
							rownumbers : true,
							singleSelect : true,
							checkOnSelect : false,
							selectOnCheck : false,
							loadMsg : '正在加载数据',
							onClickRow: function(rowIndex, rowData) {
								rowNumber=rowIndex;
							},
							onLoadSuccess:function(data){
								$('#publishitem_table').datagrid('selectRow',rowNumber);
							},
							detailFormatter:function(index,row){
								return '<div style="padding:2px"><table class="ddv"></table></div>';
							},
							onExpandRow: function(index,row){
								var ddv = $(this).datagrid('getRowDetail',index).find('table.ddv');
								ddv.datagrid({
									url:'admin/queryAdItemByCondition.do?itemProjectId='+row.projectId,
									fitColumns:true,
									singleSelect:true,
									rownumbers:true,
									loadMsg:"正在加载数据",
									height:'auto',
									columns:[[
										{field:'itemTitle',title:'回报名称',width:150},
										{field:'itemSupportMoney',title:'需筹金额',width:100},
										{field:'itemSupportMaxCount',title:'最大人数',width:100},
										{field:'itemSupportCount',title:'已支持人数',width:150},
										{field:'itemContentUrl',title:'回报内容',width:100},
										{field:'itemStatus',title:'筹集状态',width:100},
										{field:'itemSequence',title:'回报排序',width:150},
										{field:'itemUseAuthCode',title:'校验码是否使用',width:150},
										{field:'itemUseReceiveraddress',title:'是否需要收货地址',width:150},
										{field:'itemAwardStatus',title:'是否参与抽奖',width:150}
									]],
									onResize:function(){
										$('#publishitem_table').datagrid('fixDetailRowHeight',index);
									},
									onLoadSuccess:function(){
										setTimeout(function(){
											$('#publishitem_table').datagrid('fixDetailRowHeight',index);
										},0);
									},
									loadFilter : function(data) {
										for(var i=0;i<data.rows.length;i++){
											 if(data.rows[i].itemStatus=='0'){
												 data.rows[i].itemStatus='可支持'
											 }
											 if(data.rows[i].itemStatus=='1'){
												 data.rows[i].itemStatus='不可支持'
											 }
											 if(data.rows[i].itemUseAuthCode){
												 data.rows[i].itemUseAuthCode='是';
											 }else{
												 data.rows[i].itemUseAuthCode='否';
											 }
											 if(data.rows[i].itemUseReceiveraddress){
												 data.rows[i].itemUseReceiveraddress='是';
											 }else{
												 data.rows[i].itemUseReceiveraddress='否';
											 }
											 if(data.rows[i].itemAwardStatus=='1'){
													data.rows[i].itemAwardStatus='不参与';
												}
											 if(data.rows[i].itemAwardStatus=='2'){
													data.rows[i].itemAwardStatus='参与抽奖';
												}
											 if(data.rows[i].itemAwardStatus=='3'){
													data.rows[i].itemAwardStatus='已抽奖';
												}
										}
										return data;
									}
								});
								$('#publishitem_table').datagrid('fixDetailRowHeight',index);
							},
							loadFilter:function(data){
									for (var i = 0; i < data.rows.length; i++) {
									if (data.rows[i].projectStatus == 0) {
										data.rows[i].projectStatus = '未开始';
										if (data.rows[i].projectResult == 0) {
											data.rows[i].projectResult = ''
										}
									}
									if (data.rows[i].projectStatus == 1) {
										data.rows[i].projectStatus = '进行中';
										if (data.rows[i].projectResult == 0) {
											data.rows[i].projectResult = ''
										}
									}
									if (data.rows[i].projectStatus == 2) {
										data.rows[i].projectStatus = '已结束';
									}
									if (data.rows[i].projectStatus == 3) {
										data.rows[i].projectStatus = '已下架';
										if (data.rows[i].projectResult == 0) {
											data.rows[i].projectResult = ''
										}
									}
									if (data.rows[i].projectResult == 0) {
										data.rows[i].projectResult = '失败';
									} else {
										data.rows[i].projectResult = '成功';
									}
								}
								return data;
							},
							onClickRow : function(rowIndex, rowData) {

							},
							onDblClickRow : function(rowIndex, rowData) {
								//layer.msg('double');
							},
							toolbar : '#toolbar',
							columns : [ [
							/* {
								field : 'staffId',
								title : '用户id',
								width : 50,
								align : 'center'
							}, */
							{
								field : 'projectTitle',
								title : '项目标题',
								width : 50,
								align : 'center'
							}, /* {field : 'projectTitleImgUrl',*/
								{
								field : 'projectDescription',
								title : '项目简介',
								width : 50,
								align : 'center'
							}, {
								field : 'projectContentUrl',
								title : '详情地址',
								width : 50,
								align : 'center'
							}, /* {field : 'projectVideoUrl',field : 'projectVideoImgUrl',}, */
							{
								field : 'projectGoalMoney',
								title : '目标金额',
								width : 50,
								align : 'center'
							}, {
								field : 'projectSupportMoney',
								title : '已筹金额',
								width : 50,
								align : 'center'
							}, {
								field : 'projectSupportCount',
								title : '支持人数',
								width : 50,
								align : 'center'
							}, {
								field : 'projectStartTime',
								title : '开始时间',
								width : 50,
								align : 'center'
							}, {
								field : 'projectEndTime',
								title : '结束时间',
								width : 50,
								align : 'center'
							}, {
								field : 'projectStatus',
								title : '项目状态',
								width : 50,
								align : 'center'
							}, {
								field : 'projectResult',
								title : '项目结果',
								width : 50,
								align : 'center'
							}, {
								field : 'projectAttentionCount',
								title : '关注数',
								width : 50,
								align : 'center'
							}, {
								field : 'projectLikeCount',
								title : '点赞数',
								width : 50,
								align : 'center'
							}, {
								field : 'userName',
								title : '发起人',
								width : 50,
								align : 'center'
							}, {
								field : 'projectSequence',
								title : '项目靠前',
								width : 50,
								align : 'center'
							} ] ]
						});
		//设置分页控件  
		var p = $('#publishitem_table').datagrid('getPager');
		$(p).pagination({
			pageSize : 10,//每页显示的记录条数，默认为10  
			pageList : [ 5, 10, 20, 30, 40, 50 ],//可以设置每页记录条数的列表  
			beforePageText : '第',//页数文本框前显示的汉字  
			afterPageText : '页    共 {pages} 页',
			displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录',
			onSelectPage : function(pageNumber, pageSize) {
				//alert("pageNumber="+pageNumber+"pageSize="+pageSize)
				var datas = {
					projectStatus:$('#sprojectStatus').combobox('getValue'),
					projectTitle:$('#sprojectTitle').textbox('getValue'),
					userName:$('#sprojectSponsorName').textbox('getValue'),
					projectResult:$('#sprojectResult').combobox('getValue'),
					currentPage : (pageNumber - 1) * pageSize,
					pageSize : pageSize
				}
				loadData(datas);
			},
			onRefresh : function(pageNumber, pageSize) {
				var datas = {
					projectStatus:$('#sprojectStatus').combobox('getValue'),
					projectTitle:$('#sprojectTitle').textbox('getValue'),
					userName:$('#sprojectSponsorName').textbox('getValue'),
					projectResult:$('#sprojectResult').combobox('getValue'),
					currentPage : (pageNumber - 1) * pageSize,
					pageSize : pageSize
				}
				loadData(datas);
			}
		});
	});
	
	//toolbar listener
	
	$(function(){
		$('#create').bind('click',function(){
			$('#publishdlg').form('clear');
			$("#projectContentUrl").html('');
			$('#publishdlg').dialog('open')
					.dialog("setTitle", "新增项目");
			$("#projectsaveUrl").val(
					"admin/addAdProject.do");

		});

		$('#update').bind('click',function(){
			$('#publishdlg').form('clear');
			$("#projectContentUrl").html('');
			var row = $('#publishitem_table').datagrid('getSelected');
			if (row) {
				$('#publishdlg').dialog('open')
						.dialog("setTitle",
								"修改项目");
				$('#publishfm').form('load',
						row);
				$("#projectsaveUrl").val('admin/updateAdProject.do?projectId='+ row.projectId);
				loadProjectContent(row.projectContentUrl);
				$('#userId').val(row.projectSponsorId)
			} else {
				layer.msg('请选中主行！');
			}
		});
		$('#createitem').bind('click',function(){
			$('#aditemdlg').form('clear');
			$("#itemContentUrl").html('');
			var row = $('#publishitem_table')
					.datagrid('getSelected');
			if(row.projectStatus=='未开始'||row.projectStatus=='进行中'){
			if (row) {
				$("#aditemdlg").dialog('open')
						.dialog("setTitle",
								row.projectTitle+"-----添加回报");
				$("#itemsaveUrl")
						.val(
								'admin/addAditem.do?itemProjectId='
										+ row.projectId);
			} else {
				layer.msg('请选中主行');
			}
			}else{
				layer.msg('禁止添加回报');
			}
		});
		$('#delete').bind('click',function(){
			var row = $('#publishitem_table').datagrid('getSelected');
			if(row){
				if(row.projectStatus!="已下架"){
					//询问框
					layer.confirm('确定下架吗？', {
					  btn: ['确定','取消'] //按钮
					}, function(){
						$.ajax({
							url : 'admin/updateProjectByProjectStatus.do',
							type : "POST",
							dataType : "json",
							data : {
								'projectId':row.projectId,
								'projectTitle':row.projectTitle
							},
							success : function(param) {
								layer.msg(param.tips);
								$('#publishitem_table').datagrid('reload');
							}
						});
					});
				}else{
					layer.msg("项目已下架",{icon: 5});
				}
			}else{
				layer.msg("请选中下架的项目",{icon: 5});
			}
		});
		$('#previewpc').bind('click',function(){
			var host = window.location.host;
			var row = $('#publishitem_table').datagrid('getSelected');
			if(row){
				if(host.indexOf('andibaba')!=-1){//判断域名选择地址预览
					window.open('http://www.andibaba.com/v2/p/prjdetails.html?pid='+row.projectId);
				}else{
					window.open('http://test.andyage.com/v2/p/prjdetails.html?pid='+row.projectId);
				}
			}else{
				layer.msg('请选择需要预览的项目!');
			}
		});
		$('#previewmob').bind('click',function(){
			var host = window.location.host;
			var row = $('#publishitem_table').datagrid('getSelected');
			$('#code').html('');
			if(row){
				if(host.indexOf('andibaba')!=-1){//判断域名选择地址预览
					$('#code').qrcode('http://www.andibaba.com/v2/m/prjdetails.html?pid='+row.projectId); //任意字符串 
				}else{
					$('#code').qrcode('http://test.andyage.com/v2/m/prjdetails.html?pid='+row.projectId); //任意字符串
				}
				$('#code').dialog('open').dialog("setTitle", "扫描二维码");
			}else{
				layer.msg('请选择需要预览的项目!');
			}
		});
		$('#getURL').bind('click',function(){
			var row = $('#publishitem_table').datagrid('getSelected');
			if(row){
				$.ajax({
					url : 'admin/postHttpByProjectId.do',
					type : "POST",
					dataType : "json",
					data : {
						'projectId':row.projectId,
					},
					success : function(param) {
						layer.open({
							  type: 1,
							  skin: 'layui-layer-rim', //加上边框
							  area: ['600px', '250px'], //宽高
							  content: param.data,
							});
					},
					error:function(error){
						layer.open({
							  type: 1,
							  skin: 'layui-layer-rim', //加上边框
							  area: ['600px', '250px'], //宽高
							  content: error.responseText.data,
							});
					}
				});
			}
		});
	});
	//项目发布保存按钮
	function projectSave() {
		var pageSize = $('#publishitem_table').datagrid('getPager').data("pagination").options.pageSize;
		var pageNumber = $('#publishitem_table').datagrid('getPager').data("pagination").options.pageNumber;
		var datas={
				currentPage : (pageNumber - 1) * pageSize,
				pageSize:pageSize
				};
		$.ajax({
					url : $("#projectsaveUrl").val(),
					type : "POST",
					dataType : "json",
					data : {
						projectTitle : $("#projectTitle").textbox('getValue'),
						projectVideoUrl : $("#projectVideoUrl").textbox(
								'getValue'),
						projectTitleImgUrl : $("#projectTitleImgUrl").textbox(
								'getValue'),
						projectVideoImgUrl : $("#projectVideoImgUrl").textbox(
								'getValue'),
						StartTime : $("#projectStartTime").datetimebox(
								'getValue'),
						EndTime : $("#projectEndTime").datetimebox('getValue'),
						projectGoalMoney : $("#projectGoalMoney").numberbox(
								'getValue'),
						projectOrderImgUrl : $("#projectOrderImgUrl").textbox(
								'getValue'),
						projectDescription : $("#projectDescription").textbox(
								'getValue'),
						projectContent : projectContentUrl.getContent(),
						//projectContent:projectContentUrl.getPlainTxt(),
						projectSponsorId : $("#userId").val(),
						projectSequence:$('#projectSequence').numberbox('getValue'),
					},
					beforeSend : function() {
						var flag=$("#publishfm").form('enableValidation').form('validate');
						if(flag){
							$('#projectSave').linkbutton({disabled:true});  
							return flag;
						}else{
							return flag;
						}
					},
					success : function(result) {
						$('#projectSave').linkbutton({disabled:false});    
						//var result = eval('('+result+')');
						//alert(result);
						if (result.result == 0) {
							layer.msg(result.tips);
							//alert(result.tips);
						} else {
							layer.msg(result.tips);
							$('#publishdlg').dialog('close'); // close the dialog
							$('#publishitem_table').datagrid('reload',datas); // reload the user data
						}

					}
				});
	}
	//取消按钮
	function projectCancel() {
		$('#projectCancel').linkbutton({disabled:false}); 
		$('#publishdlg').dialog('close');
		$('#publishfm').form('clear');
	}
	//读取项目详情
	function loadProjectContent(projectURL) {
		//layer.msg('正在加载项目详情...');
		//var value = prompt('插入html代码', '');
		//projectContentUrl.execCommand('insertHtml', projectURL)
		$("#projectContentUrl").load(projectURL);
		layer.msg('项目详情加载完毕..');
	}
	//保存子项目
	function itemSave() {
		$.ajax({
					url : $("#itemsaveUrl").val(),
					type : "POST",
					dataType : "json",
					data : {
						itemSequence:$("#itemSequence").numberbox('getValue'),
						itemSupportMoney : $("#itemSupportMoney").numberbox(
								'getValue'),
						itemSupportMaxCount : $("#itemSupportMaxCount")
								.numberbox('getValue'),
						itemContent : itemContentUrl.getContent(),
						itemTitle : $("#itemTitle").textbox('getValue'),
						itemUseReceiveraddress : $("#itemUseReceiveraddress").combobox('getValue'),
						itemAwardStatus:$('#itemAwardStatus').combobox('getValue')
					},
					beforeSend : function() {
						var flag=$("#itemfm").form('enableValidation').form('validate');
						if(flag){
							$('#itemSave').linkbutton({disabled:true}); 
							return flag;
						}else{
							return flag;
						}
						
					},
					success : function(result) {
						$('#itemSave').linkbutton({disabled:false});
						if (result.result == 0) {
							layer.msg(result.tips);
						} else {
							layer.msg(result.tips);
							$('#aditemdlg').dialog('close'); // close the dialog
							$('#publishitem_table').datagrid('reload'); // reload the user data
						}

					}
				});
	}
	//子项目的取消按钮
	function itemCancel() {
		$('#itemSave').linkbutton({disabled:false});
		$('#aditemdlg').dialog('close');
		$('#itemfm').form('clear');
	}
	//load data
	var loadData = function(datas) {
		$.ajax({
			url : 'admin/queryAdProject.do',
			type : "POST",
			dataType : "json",
			data : datas,
			success : function(param) {
				$('#publishitem_table').datagrid('loadData', param);
			}
		});
	};
	//search btn
	$(function(){
		$('#searchProjectBtn').bind('click',function(){
			var pageSize=$('#publishitem_table').datagrid('getPager').data("pagination").options.pageSize;
			var datas={
					projectStatus:$('#sprojectStatus').combobox('getValue'),
					projectTitle:$('#sprojectTitle').textbox('getValue'),
					userName:$('#sprojectSponsorName').textbox('getValue'),
					projectResult:$('#sprojectResult').combobox('getValue'),
					pageSize:pageSize,
			}
			loadData(datas);
		});		
	});
	
	//reset btn
	$(function(){
		$('#resetProjecttn').bind('click',function(){
			$('#sprojectStatus').combobox('setValue',''),
			$('#sprojectTitle').textbox('setValue','');	
			$('#sprojectSponsorName').textbox('setValue','');
			$('#sprojectResult').combobox('setValue','');
			var pageSize=$('#publishitem_table').datagrid('getPager').data("pagination").options.pageSize;
			var datas={
					pageSize:pageSize,
			}
			loadData(datas);
		});	
	});